package com.situ.springbootscientific.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.github.pagehelper.page.PageMethod;
import com.situ.springbootscientific.mapper.ProjectMapper;
import com.situ.springbootscientific.pojo.Project;
import com.situ.springbootscientific.pojo.query.ProjectQuery;
import com.situ.springbootscientific.pojo.vo.ProjectVo;
import com.situ.springbootscientific.service.IProjectService;
import com.situ.springbootscientific.utils.PageRusults;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ProjectServiceImpl implements IProjectService {
    @Autowired
    ProjectMapper projectMapper;
    @Override
    public PageRusults list(ProjectQuery projectQuery) {
        int limit = projectQuery.getLimit()!= null? projectQuery.getLimit() : 10; // 设置默认值为 10，如果 limit 为 null
        int page = projectQuery.getPage()!= null? projectQuery.getPage() : 1;

        PageHelper.startPage(page, limit);
        //1.查询当前页列表
        List<ProjectVo> projectVoList = projectMapper.list(projectQuery);
        //2.用一个小插件查询总数量
        PageInfo pageInfo = new PageInfo(projectVoList);
        int total = (int) pageInfo.getTotal();
        return new PageRusults(0,"",total,projectVoList);
    }

    @Override
    public void update(Project project) {
        projectMapper.update(project);
    }

    @Override
    public ProjectVo selectById(Integer id) {
        ProjectVo projectVo = projectMapper.selectById(id);
        return projectVo;
    }


    @Override
    public List<ProjectVo> selectByRes(Integer id) {

        //1.查询当前页列表
        List<ProjectVo> projectVoList = projectMapper.selectByRes(id);

        return projectVoList;
    }

    @Override
    public void add(ProjectVo projectVo) {
        projectMapper.add(projectVo);
    }
}
